package com.kbbywyclient.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.data.entity.MyBusinessCon;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.MyBusinessConModule;
import com.kbbywyclient.presenter.MyBusinessConPresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.MyBusinessConContarct;
import com.kbbywyclient.util.CheckFormat;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.ToastUtil;
import com.kbbywyclient.util.ViewUtils;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.CustomDialog;
import com.kbbywyclient.widget.CustomToolBar;
import com.squareup.picasso.Picasso;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description:
 * Created by FHW
 * on 2017/5/11
 */

public class MyBusinessConActivity extends BaseActivity implements MyBusinessConContarct.View {

    @BindView(R.id.v_toolbar)
    CustomToolBar v_toolbar;

    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_mobile)
    TextView tv_mobile;
    @BindView(R.id.tv_wechat)
    TextView tv_wechat;
    @BindView(R.id.iv_two_code)
    ImageView iv_two_code;
    @BindView(R.id.rl_qq)
    RelativeLayout rl_qq;

    @Inject
    MyBusinessConPresenter myBusinessConPresenter;
    private String qq = "";
    private String mobile = "";

    private CustomDialog dialog;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_business_con;
    }

    @OnClick({R.id.tv_qq, R.id.tv_mobile})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_qq:
                if (CheckFormat.checkApkExist(this, "com.tencent.mobileqq")) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqwpa://im/chat?chat_type=wpa&uin=" + qq + "&version=1")));
                    setScreenAction("通信", "业务顾问-qq交谈");
                } else {
                    ToastUtil.showShort("本机未安装QQ应用");
                }
                break;
            case R.id.tv_mobile:
                showInfoDialog();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("我的业务顾问");
    }

    @Override
    protected void initView() {
        v_toolbar.setLeftViewOnClickLinster(v -> finish());
    }

    @Override
    public void initData() {
        myBusinessConPresenter.getMyBusinessCon(PreUtils.getUser(this).getSalesID() + "");
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getMyBusinessConComponent(new MyBusinessConModule(this)).inject(this);
    }

    /**
     *
     */
    private void showInfoDialog() {
        dialog = new CustomDialog(this, R.style.Customdialog, "", mobile
                , new CustomDialog.ButtonListener() {
            @Override
            public void left() {
                dialog.dismiss();
            }

            @Override
            public void right() {
                ViewUtils.dialPhoneNumber(MyBusinessConActivity.this, mobile);
                setScreenAction("通信", "业务顾问-拨打电话");
                dialog.dismiss();
            }
        }, getString(R.string.cancel), "呼叫");
        dialog.show();
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void loadSucBusinessCon(MyBusinessCon.MySalesBean mySales) {
        qq = mySales.getQQ();
        mobile = mySales.getPHONE();

        if (StringUtil.isNotEmpty(qq)) {
            rl_qq.setVisibility(View.VISIBLE);
        } else {
            rl_qq.setVisibility(View.GONE);
        }
        ViewUtils.setText(tv_name, mySales.getNAME());
        ViewUtils.setText(tv_mobile, mobile);
        ViewUtils.setText(tv_wechat, mySales.getWeiXinID());
        Picasso.with(MyBusinessConActivity.this).load(API.PIC_ADDRESS + mySales.getWeiXinImage()).resize(180, 180).centerCrop().into(iv_two_code);
    }

    @Override
    public void loadFaiBusinessCon(String errorMsg) {

    }
}
